
import * as THREE from 'three';
const PULSE_FREQUENCY_COUNT = 200;
const ENTER_FROM_POSITION = {
  r: 0,
  a: Math.PI * 2,
  y: 600
};
const ENTER_TO_POSITION = {
  r: 0,
  a: 0,
  y: 0
}
const ENTER_FROM_ORIENTATION = {
  r: 38,
  a: 0.9,
  y: 0,
  z: 0
}
const ENTER_TO_ORIENTATION = {
  r: 200,
  a: Math.PI * 2 - Math.PI * 0.5,
  y: 90,
  z: 0
}

const CHASE_START_ORIENTATION = {
  x: THREE.Math.radToDeg(Math.atan2(Math.sin(ENTER_TO_ORIENTATION.a) + ENTER_TO_ORIENTATION.r * Math.cos(ENTER_TO_ORIENTATION.a), Math.cos(ENTER_TO_ORIENTATION.a) - ENTER_TO_ORIENTATION.r * Math.sin(ENTER_TO_ORIENTATION.a))),
  y: ENTER_TO_ORIENTATION.y,
  z: ENTER_TO_ORIENTATION.z
}

class AnimateImpulse{
  constructor(fromValues, toValues, offset, animateCallback, finishAnimate){
    this._fromValues = fromValues
    this._toValues = toValues
    this._offsetScale = 1 / offset;
    this._animateCallback = animateCallback;
    this._currentValues = fromValues.concat();
    this._isFinished = false;
    let offsetValues = []
    this._fromValues.forEach((from, index) => {
      if (typeof from === 'object'){
        let signObject = {};
        for (let key in from){
          if (from.hasOwnProperty(key)){
            signObject[key] = toValues[index][key] - from[key];
          }
        }
        offsetValues.push(signObject);
      }
      if (typeof from === 'number'){
        let sign = toValues[index] - from;
        offsetValues.push(sign);
      }
    });
    this._offsetValues = offsetValues;
    this.finishAnimate = finishAnimate;
  }

  setFromValues(values){
    this._startValues = values;
  }

  setToValues(values){
    this._toValues = values;
  }

  update(){
    if (this._isFinished){
      return;
    }
    let currentFirstValue = this._currentValues[0];
    if (typeof currentFirstValue === 'number'){
      if ((this._offsetValues[0] < 0 && currentFirstValue < this._toValues[0]) ||
        this._offsetValues[0] > 0 && currentFirstValue > this._toValues[0]){
        this._isFinished = true;
        if (this.finishAnimate){
          this.finishAnimate();
        }
        return;
      }
    }

    if (typeof currentFirstValue === 'object'){
      for (let key in currentFirstValue){
        if ((this._offsetValues[0][key] < 0 && currentFirstValue[key] < this._toValues[0][key]) ||
          this._offsetValues[0][key] > 0 && currentFirstValue[key] > this._toValues[0][key]){
          this._isFinished = true;
          if (this.finishAnimate){
            this.finishAnimate();
          }
          return;
        }
      }
    }
    let _offsetValues = this._offsetValues;
    this._currentValues = this._currentValues.map((value, index) => {
      if (typeof value === 'number'){
        return value + _offsetValues[index] * this._offsetScale;
      }
      if (typeof value === 'object'){
        let nextObject = {};
        for (let key in value){
          if (value.hasOwnProperty(key)){
            nextObject[key] = value[key] + _offsetValues[index][key] * this._offsetScale;
          }
        }
        return nextObject;
      }
    });
    if (this._animateCallback){
      this._animateCallback(this._currentValues);
    }
  }
}

export default AnimateImpulse;

export {
  PULSE_FREQUENCY_COUNT,
  ENTER_FROM_POSITION,
  ENTER_TO_POSITION,
  ENTER_FROM_ORIENTATION,
  ENTER_TO_ORIENTATION,
  CHASE_START_ORIENTATION
};
